package com.system.cacheClient.client.impl;

import com.system.cacheClient.client.HashCacheClient;
import com.system.cacheClient.common.CacheR;
import com.system.cacheClient.feign.HashCacheFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Hash缓存客户端实现类
 */
@Component
@RequiredArgsConstructor
public class HashCacheClientImpl implements HashCacheClient {
    
    private final HashCacheFeignClient hashCacheFeignClient;
    
    @Override
    public <T> CacheR<Boolean> hset(String key, String hashKey, T value) {
        // 先获取整个Hash
        CacheR<Map<String, String>> getResult = hashCacheFeignClient.get(key);
        if (getResult.getCode() != 200) {
            return CacheR.fail("获取Hash失败");
        }
        
        Map<String, String> hash = getResult.getData();
        if (hash == null) {
            hash = new HashMap<>();
        }
        
        // 更新指定字段
        hash.put(hashKey, value.toString());
        
        // 重新设置整个Hash
        return hashCacheFeignClient.set(key, hash);
    }
    
    @Override
    public <T> CacheR<T> hget(String key, String hashKey) {
        CacheR<Map<String, String>> result = hashCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            String value = result.getData().get(hashKey);
            return (CacheR<T>) CacheR.success(value);
        }
        return (CacheR<T>) CacheR.fail("获取Hash字段失败");
    }
    
    @Override
    public CacheR<Long> hdel(String key, String... hashKeys) {
        // 先获取整个Hash
        CacheR<Map<String, String>> getResult = hashCacheFeignClient.get(key);
        if (getResult.getCode() != 200 || getResult.getData() == null) {
            return CacheR.fail(0L, "获取Hash失败");
        }
        
        Map<String, String> hash = getResult.getData();
        long deletedCount = 0;
        
        // 删除指定字段
        for (String hashKey : hashKeys) {
            if (hash.remove(hashKey) != null) {
                deletedCount++;
            }
        }
        
        // 重新设置整个Hash
        CacheR<Boolean> setResult = hashCacheFeignClient.set(key, hash);
        if (setResult.getCode() == 200 && setResult.getData()) {
            return CacheR.success(deletedCount, "删除Hash字段成功");
        }
        return CacheR.fail(0L, "删除Hash字段失败");
    }
    
    @Override
    public CacheR<Set<String>> hkeys(String key) {
        CacheR<Map<String, String>> result = hashCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            Set<String> keys = result.getData().keySet();
            return CacheR.success(keys, "获取Hash键集合成功");
        }
        return CacheR.fail("获取Hash键集合失败");
    }
    
    @Override
    public CacheR<List<String>> hvals(String key) {
        CacheR<Map<String, String>> result = hashCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            List<String> values = new ArrayList<>(result.getData().values());
            return CacheR.success(values, "获取Hash值集合成功");
        }
        return CacheR.fail("获取Hash值集合失败");
    }
    
    @Override
    public CacheR<Map<String, String>> hgetall(String key) {
        return hashCacheFeignClient.get(key);
    }
    
    @Override
    public CacheR<Long> hlen(String key) {
        CacheR<Map<String, String>> result = hashCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            long size = result.getData().size();
            return CacheR.success(size, "获取Hash大小成功");
        }
        return CacheR.fail(0L, "获取Hash大小失败");
    }
}